package com.admin.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import com.mysql.jdbc.PreparedStatement;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.ConnectionInfo;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class Linux {
public static void getSystem(String ip){
	SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String now=sd.format(new Date());
	  com.mysql.jdbc.Connection connection_mysql;
	try {
		connection_mysql = JDBCTools.getConnection();
	
		 PreparedStatement preparedStatement = null;  
	 
          Connection con = new Connection(ip);
          ConnectionInfo info = con.connect();
          boolean result = con.authenticateWithPassword("root", "123456");
          Session sess = con.openSession();
          sess.execCommand("df -h");
          InputStream stdout = new StreamGobbler(sess.getStdout());
          BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
          String line;
          while ((line = br.readLine())!=null) { 
        	   if(line.contains("/dev/dm-0")){
        		   line=line.replace("/dev/dm-0", "").replace("/", "").trim();
        		  String [] strs=line.split("\\s+");
        		  // System.out.println("磁盘使用：     ip："+ip+",当前值："+strs[1]+",最高值：40，时间："+now);
        		  
        			   String	sql="insert into admin_ypjc(id,ip,dqz,zgz,time) values('"+UUID.randomUUID().toString().replace("-", "")+"','"+ip+"','"+strs[1].replace("G", "")+"','40','"+now+"')";
        				preparedStatement = (PreparedStatement) connection_mysql.prepareStatement(sql);  
        				preparedStatement.executeUpdate(); 
        				
        		 
        		  break;
        	  // System.out.println(line);
        	   }
        	} 
       //   System.out.println("ExitCode: " + sess.getExitStatus());
          sess = con.openSession();
          sess.execCommand("top -bn 1 -i -c");
          InputStream stdout1 = new StreamGobbler(sess.getStdout());
          BufferedReader br1= new BufferedReader(new InputStreamReader(stdout1));
          String line1;
          while ((line1 = br1.readLine())!=null) { 
        	  if(line1.contains("%Cpu(s)")){
        		  String [] strs=line1.split("\\s+");
        		  double d=100-Double.valueOf(strs[7]);
        		  BigDecimal bg = new BigDecimal(d);  
                  double f1 = bg.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();  
        		 // System.out.println("cpu使用：     ip："+ip+",当前值："+f1+",最高值：100，时间："+now);
        			
        			   String	sql1="insert into admin_cpujc(id,ip,dqz,zgz,time) values('"+UUID.randomUUID().toString().replace("-", "")+"','"+ip+"','"+f1+"','100','"+now+"')";
        				preparedStatement = (PreparedStatement) connection_mysql.prepareStatement(sql1);  
        				preparedStatement.executeUpdate(); 
        		 
        	   break;
        	  }
          } 
          sess = con.openSession();
          sess.execCommand("free");
          InputStream stdout2 = new StreamGobbler(sess.getStdout());
          BufferedReader br2= new BufferedReader(new InputStreamReader(stdout2));
          String line2;
          while ((line2 = br2.readLine())!=null) { 
        	  if(line2.contains("-/+ buffers/cache:")){
        		 double d=Double.valueOf(line2.split("\\s+")[2])/1024;
        		 int f= (new   Double(d)).intValue();  
                // System.out.println("内存使用：     ip："+ip+",当前值："+f+",最高值：4096，时间："+now);
                 String	sql1="insert into admin_ncjc(id,ip,dqz,zgz,time) values('"+UUID.randomUUID().toString().replace("-", "")+"','"+ip+"','"+f+"','4096','"+now+"')";
 				preparedStatement = (PreparedStatement) connection_mysql.prepareStatement(sql1);  
 				preparedStatement.executeUpdate(); 
        				 break;
        	  }
          } 
          preparedStatement.close();
          connection_mysql.close();
          br.close();
          br1.close();
          br2.close();
          sess.close();
          con.close();
      } catch (Exception ex) {
          System.out.println(ex.getLocalizedMessage());
      }
  }
public static void jcxn(){
	getSystem("192.168.0.219");
	getSystem("192.168.0.220");
	getSystem("192.168.0.221");
	getSystem("192.168.0.223");
}
public static void main(String[] args) {
	//getSystem("192.168.0.219");
	jcxn();
}
}
